using System;
using System.Collections;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Windows.Forms;

namespace WindowsControl
{
	/// <summary>
	/// Summary description for UserControl1.
	/// </summary>
	public class ControlBar : System.Windows.Forms.Form
	{
		/// <summary>
		/// Required designer variable.
		/// </summary>
		public delegate void ButtonPressHandler(object sender,string e);
		public event ButtonPressHandler ButtonPress;
		protected virtual void OnButtonPressEvent(string e) 
		{
			if (ButtonPress != null) ButtonPress(this, e);
		}
		private int listCtr = 0;
		private float oldWidth = 0;
		private float formScale = 1;
		public ArrayList mouseFormList = new ArrayList();
		private System.Windows.Forms.CheckBox chkLayout;
		private System.Windows.Forms.Button btnKey;
		private System.Windows.Forms.CheckBox ChkTrans;
		private System.Windows.Forms.Button btnFwd;
		private System.Windows.Forms.Button btnBack;
		private System.ComponentModel.Container components = null;
		private void SetAlpha(object sender, System.EventArgs e)
		{
			double val = 100;
			if (ChkTrans.Checked){val = 0.5;}
			foreach( MouseForm obj in mouseFormList)
			{
				obj.Opacity = val;
			}
		}
		public void AutoPosition()
		{
			int space = 10;
			int x = 0;
			int y = this.Size.Height + space;
			int maxY = 0;
			foreach( MouseForm obj in mouseFormList)
			{
				obj.Offset = new Point(x,y);
				if (obj.Size.Height > maxY){maxY = obj.Size.Height; }
				x += obj.Size.Width + space;
				if (x > this.Size.Width)
				{
					x = 0; 
					y += maxY + space;
					maxY = 0;
				}
			}	
			oldWidth = (float)(this.Size.Width)/formScale;
			MoveMouseForms();
		}
		public void AutoFormScale()
		{
			formScale = (float)(this.Size.Width )/oldWidth;
			foreach( MouseForm obj in mouseFormList)
			{
				obj.ScaleSize(formScale);
			}
			AutoPosition();
		}
		private void MoveMouseForms()
		{
			foreach( MouseForm obj in mouseFormList)
			{
				obj.NewLocation( this.Location);
			}
		}
		public MouseForm addForm()
		{
			MouseForm F = new MouseForm();
			F.Show();
			F.formIndex = listCtr ++;
			mouseFormList.Add(F);
			AutoPosition();
			oldWidth = (float)(this.Size.Width)/formScale;
			return F;
		}
		public ControlBar()
		{
			// This call is required by the Windows.Forms Form Designer.
			InitializeComponent();
			// TODO: Add any initialization after the InitComponent call
		}

		/// <summary>
		/// Clean up any resources being used.
		/// </summary>
		protected override void Dispose( bool disposing )
		{
			if( disposing )
			{
				if( components != null )
					components.Dispose();
			}
			base.Dispose( disposing );
		}
		#region Component Designer generated code
		/// <summary>
		/// Required method for Designer support - do not modify 
		/// the contents of this method with the code editor.
		/// </summary>
		private void InitializeComponent()
		{
			this.chkLayout = new System.Windows.Forms.CheckBox();
			this.btnKey = new System.Windows.Forms.Button();
			this.ChkTrans = new System.Windows.Forms.CheckBox();
			this.btnFwd = new System.Windows.Forms.Button();
			this.btnBack = new System.Windows.Forms.Button();
			this.SuspendLayout();
			// 
			// chkLayout
			// 
			this.chkLayout.Checked = true;
			this.chkLayout.CheckState = System.Windows.Forms.CheckState.Checked;
			this.chkLayout.Location = new System.Drawing.Point(12, 47);
			this.chkLayout.Name = "chkLayout";
			this.chkLayout.Size = new System.Drawing.Size(64, 16);
			this.chkLayout.TabIndex = 0;
			this.chkLayout.Text = "Layout";
			// 
			// btnKey
			// 
			this.btnKey.Font = new System.Drawing.Font("Microsoft Sans Serif", 10F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
			this.btnKey.Location = new System.Drawing.Point(68, 0);
			this.btnKey.Name = "btnKey";
			this.btnKey.Size = new System.Drawing.Size(50, 30);
			this.btnKey.TabIndex = 1;
			this.btnKey.Text = "Key";
			// 
			// ChkTrans
			// 
			this.ChkTrans.Checked = true;
			this.ChkTrans.CheckState = System.Windows.Forms.CheckState.Checked;
			this.ChkTrans.Location = new System.Drawing.Point(90, 47);
			this.ChkTrans.Name = "ChkTrans";
			this.ChkTrans.Size = new System.Drawing.Size(96, 16);
			this.ChkTrans.TabIndex = 2;
			this.ChkTrans.Text = "Transparent";
			this.ChkTrans.CheckStateChanged += new System.EventHandler(this.SetAlpha);
			// 
			// btnFwd
			// 
			this.btnFwd.Font = new System.Drawing.Font("Microsoft Sans Serif", 16F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
			this.btnFwd.Location = new System.Drawing.Point(162, 0);
			this.btnFwd.Name = "btnFwd";
			this.btnFwd.Size = new System.Drawing.Size(24, 30);
			this.btnFwd.TabIndex = 3;
			this.btnFwd.Text = ">";
			this.btnFwd.Click += new System.EventHandler(this.ClickBtnFwd);
			// 
			// btnBack
			// 
			this.btnBack.Font = new System.Drawing.Font("Microsoft Sans Serif", 16F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
			this.btnBack.Location = new System.Drawing.Point(0, 0);
			this.btnBack.Name = "btnBack";
			this.btnBack.Size = new System.Drawing.Size(24, 30);
			this.btnBack.TabIndex = 4;
			this.btnBack.Text = "<";
			this.btnBack.Click += new System.EventHandler(this.ClickBtnBack);
			// 
			// ControlBar
			// 
			this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
			this.BackColor = System.Drawing.SystemColors.AppWorkspace;
			this.ClientSize = new System.Drawing.Size(486, 69);
			this.Controls.Add(this.btnBack);
			this.Controls.Add(this.btnFwd);
			this.Controls.Add(this.ChkTrans);
			this.Controls.Add(this.chkLayout);
			this.Controls.Add(this.btnKey);
			this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.SizableToolWindow;
			this.MaximizeBox = false;
			this.Name = "ControlBar";
			this.TopMost = true;
			this.Resize += new System.EventHandler(this.SizeForm);
			this.Closing += new System.ComponentModel.CancelEventHandler(this.CloseForms);
			this.Move += new System.EventHandler(this.MoveForm);
			this.ResumeLayout(false);

		}

		#endregion

		private void MoveForm(object sender, System.EventArgs e)
		{
			MoveMouseForms();
		}
		private void SizeForm(object sender, System.EventArgs e)
		{
			if (chkLayout.Checked)
			{
				AutoPosition();
				oldWidth = (float)(this.Size.Width)/formScale;
			}
			else {AutoFormScale();}
		}
		private void CloseForms(object sender, System.ComponentModel.CancelEventArgs e)
		{
			foreach(MouseForm obj in mouseFormList)
			{
				obj.Close();
			}
		}

		private void ClickBtnFwd(object sender, System.EventArgs e)
		{
			OnButtonPressEvent("Next");
		}

		private void ClickBtnBack(object sender, System.EventArgs e)
		{
			OnButtonPressEvent("Prev");
		}
	}

}

